import { DEVICE_CONTROLLER_WIDTH } from "@/utils";
const chargingCar = {
  type: "Charging",
  index: 0,
  hasChildren: false,
  hasPermission: false,
  height: 360,
  status: {
    texts: ["Off", "On"],
    commands: [
      [{ text: "On", command: "On" }],
      [{ text: "Off", command: "Off" }],
    ],
  },
  rules: [
    {
      key: "status",
      value: 0,
      commands: [
        { text: "On", command: "On" },
        { text: "Off", command: "Off" },
      ],
    },
    {
      key: "status",
      value: 1,
      commands: [
        { text: "On", command: "On" },
        { text: "Off", command: "Off" },
      ],
    },
  ],
};
const undefinedDevice = {
  index: -1,
  type: "Device",
  hasChildren: false,
  hasPermission: false,
  with: 200,
  height: 200,
};
export const controllerOptions = [
  chargingCar, //0
  {
    index: 1,
    type: "ICharging",
    hasChildren: false,
    hasPermission: true,
    height: 360,
  },
  {
    index: 2,
    type: "Locker",
    hasChildren: true,
    hasPermission: true,
    haveManager: true,
    height: 640,
  },
  {
    index: 3,
    type: "BleGateway",
    hasChildren: false,
    hasPermission: false,
    height: 200,
  },
  {
    index: 4,
    code: "PM25",
    type: "Environment",
    hasChildren: false,
    hasPermission: false,
    supportGroup: true,
  },
  {
    index: 5,
    code: "TH",
    type: "Environment",
    hasChildren: false,
    hasPermission: false,
    supportGroup: true,
  },
  {
    index: 6,
    code: "TVOC",
    type: "Environment",
    hasChildren: false,
    hasPermission: false,
    supportGroup: true,
  },
  {
    index: 7,
    code: "Sensor",
    type: "Environment",
    hasChildren: false,
    hasPermission: false,
    supportGroup: true,
  },
  {
    index: 8,
    type: "OnOff",
    hasChildren: false,
    hasPermission: false,
    supportGroup: false,
    width: DEVICE_CONTROLLER_WIDTH,
  },
  {
    index: 9,
    type: "Air",
    hasChildren: false,
    hasPermission: false,
    supportGroup: false,
    width: DEVICE_CONTROLLER_WIDTH,
  },
  {
    index: 10,
    type: "Curtain",
    hasChildren: false,
    hasPermission: false,
    haveManager: true,
    supportGroup: false,
    width: DEVICE_CONTROLLER_WIDTH,
  },
  {
    index: 11,
    type: "Lamp",
    hasChildren: false,
    hasPermission: false,
    supportGroup: false,
    haveManager: true,
    width: DEVICE_CONTROLLER_WIDTH,
  },
  {
    index: 12,
    type: "Screen",
    hasChildren: false,
    hasPermission: false,
    supportGroup: false,
    width: DEVICE_CONTROLLER_WIDTH,
  },
  {
    index: 13,
    type: "AuthLock",
    hasChildren: false,
    hasPermission: false,
  },
  {
    index: 14,
    type: "Camera",
    hasChildren: false,
    hasPermission: false,
    width: DEVICE_CONTROLLER_WIDTH,
    supportGroup: false,
  },
  {
    index: 15,
    type: "Platform",
    hasChildren: false,
    hasPermission: true,
    haveManager: true,
    width: 900,
    height: 300,
  },
  {
    index: 16,
    type: "Power",
    hasChildren: false,
    hasPermission: false,
  },
  {
    index: 17,
    type: "Control",
    hasChildren: false,
    hasPermission: false,
    width: 900,
    height: 300,
  },
  {
    index: 18,
    type: "AirSwitch",
    hasChildren: false,
    hasPermission: false,
    width: 300,
    height: 300,
  },
  {
    index: 19,
    type: "Control0402",
    hasChildren: false,
    hasPermission: false,
    width: 360,
    height: 300,
  },
  {
    index: 20,
    code: "Charging",
    type: "Cart",
    hasChildren: false,
    hasPermission: false,
    width: 360,
    height: 300,
  },
  {
    index: 21,
    code: "Computer",
    type: "WindowIot",
    hasChildren: false,
    hasPermission: false,
    width: 360,
    height: 300,
  },
];

const undefinedType = function (type) {
  return (
    type === undefined ||
    type === null ||
    type === "" ||
    type < 0 ||
    type >= controllerOptions.length ||
    !controllerOptions[type] ||
    !controllerOptions[type].type
  );
};
export const paramtersByType = function (type) {
  if (undefinedType(type)) {
    return undefinedDevice;
  }
  return controllerOptions[type];
};

export const codeByType = function (intType) {
  if (undefinedType(intType)) {
    return "Device";
  }
  const { type, code } = controllerOptions[intType];
  return code === undefined || code === null ? type : code;
};

export const controllerByType = function (type) {
  if (undefinedType(type)) {
    return "DeviceController";
  }
  return controllerOptions[type].type + "Controller";
};

export const managerByType = function (type) {
  if (undefinedType(type)) {
    return undefined;
  }
  const option = controllerOptions[type];
  if (option.haveManager) {
    return option.type + "Manager";
  }
  return undefined;
};

export const controllerWidthByType = function (type) {
  const defaultWidth = 420;
  if (undefinedType(type)) {
    return defaultWidth;
  }
  return controllerOptions[type].width || defaultWidth;
};
export const controllerHeightByType = function (type) {
  const defaultHeight = 140;
  if (undefinedType(type)) {
    return defaultHeight;
  }
  return controllerOptions[type].height || defaultHeight;
};
export const supportGroupByType = function (type) {
  if (undefinedType(type)) {
    return true;
  }
  return controllerOptions[type].supportGroup;
};

export const stateTextByType = function (type) {
  if (undefinedType(type)) {
    return [];
  }
  const config = controllerOptions[type];
  if (!config.status) {
    return [];
  }
  return config.status.texts || [];
};

export const stateCommandByType = function (type, status) {
  if (undefinedType(type) || (!status && status !== 0)) {
    return [];
  }
  const config = controllerOptions[type];
  if (!config.status) {
    return [];
  }
  const { commands } = config.status;
  if (!commands || status >= commands.length) {
    return [];
  }
  return commands[status];
};

export const nameByDevice = function (device) {
  if (device.type !== 11) {
    return device.name;
  }
  if (!device.name) {
    return null;
  }
  const array = device.name.split("|");
  if (array.length < 4) {
    return null;
  }
  return array[3];
};

export const deviceRuleCommand = function (device) {
  if (undefinedType(device.type)) {
    return [];
  }
  const config = controllerOptions[device.type];
  if (!config.rules || config.rules.length === 0) {
    return [];
  }
  const { rules } = config;
  const commands = [];
  for (var i = 0; i < rules.length; i++) {
    const rule = rules[i];
    if (device[rule.key] == rule.value) {
      rule.commands.forEach((item) => {
        commands.push(item);
      });
    }
  }
  return commands;
};

export const DeviceType = {
  CHARGING: 0,
  I_CHARGING: 1,
  LOCKER: 2,
  BLE_GATEWAY: 3,
  PM_25: 4,
  TEMPERATURE_HUMIDITY: 5,
  TVOC: 6,
  SENSOR: 7,
  ON_OFF: 8,
  AIR_CONDITION: 9,
  CURTAIN: 10,
  LIGHT: 11,
  SCREEN: 12,
  AUTH_LOCK: 13,
  CAMERA: 14,
  PLATFORM: 15, //讲台
  POWER: 16,
  CONTROL: 17, //中控
  AIR_SWITCH: 18,
  CONTROL_0402: 19,
};

export const getCategoryName = function (type) {
  switch (type) {
    case DeviceType.CHARGING:
      return "device_type.charging";
    case DeviceType.I_CHARGING:
      return "device_type.icharging";
    case DeviceType.LOCKER:
      return "device_type.locker";
    case DeviceType.BLE_GATEWAY:
      return "device_type.ble_gateway";
    case DeviceType.PM_25:
      return "device_type.pm25";
    case DeviceType.TEMPERATURE_HUMIDITY:
      return "device_type.temp_humidity";
    case DeviceType.TVOC:
      return "device_type.tovc";
    case DeviceType.SENSOR:
      return "device_type.sensor";
    case DeviceType.ON_OFF:
      return "device_type.on_off";
    case DeviceType.AIR_CONDITION:
      return "device_type.air_condition";
    case DeviceType.CURTAIN:
      return "device_type.curtain";
    case DeviceType.LIGHT:
      return "device_type.light";
    case DeviceType.SCREEN:
      return "device_type.screen";
    case DeviceType.AUTH_LOCK:
      return "device_type.auth_lock";
    case DeviceType.CAMERA:
      return "device_type.camera";
    case DeviceType.PLATFORM:
      return "device_type.platform";
    case DeviceType.POWER:
      return "device_type.power";
    case DeviceType.CONTROL:
      return "device_type.control";
  }
  return null;
};

export const parseBLE = function (type) {
  switch (type) {
    case 2:
      return DeviceType.LIGHT;
    case 4:
      return DeviceType.CURTAIN;
    case 6:
      return DeviceType.ON_OFF;
    case 8:
      return DeviceType.SENSOR;
    case 11:
    case 15:
      return DeviceType.AIR_CONDITION;
    case 16:
      return DeviceType.SCREEN;
    case 17:
      return DeviceType.CONTROL;
    case 100:
      return DeviceType.PLATFORM;
  }
  return null;
};

export const parseType = function (code) {
  if (code === null || code.trim() === "") {
    return null;
  }
  if (code.indexOf("PM_25") >= 0) {
    return DeviceType.PM_25;
  }
  if (code.indexOf("loT-AC-") >= 0) {
    return DeviceType.CHARGING;
  }
  if (code.indexOf("IoT-AC-") >= 0) {
    return DeviceType.CHARGING;
  }
  if (code.indexOf("WebRTC_") >= 0) {
    return DeviceType.CAMERA;
  }
  //        if (code.indexOf("locker_") >= 0) {
  //            return LOCKER;
  //        }
  if (code.indexOf("IoT-Locker-") >= 0) {
    return DeviceType.LOCKER;
  }
  if (code.indexOf("IoT-iCharger-") >= 0) {
    return DeviceType.I_CHARGING;
  }
  if (code.indexOf("LOCK_") >= 0) {
    return DeviceType.AUTH_LOCK;
  }
  if (code.indexOf("TH_RELAY") >= 0) {
    return DeviceType.TEMPERATURE_HUMIDITY;
  }
  if (code.indexOf("WebRTC_") >= 0) {
    return DeviceType.CAMERA;
  }
  if (code.indexOf("Camera_") >= 0) {
    return DeviceType.CAMERA;
  }
  if (code.indexOf("TVOC_") >= 0) {
    return DeviceType.TVOC;
  }
  //        if (code.indexOf("ROW_RELAY")) {
  //            return AIR_CONDITION;
  //        }
  if (code.indexOf("IR_CONTROL") >= 0) {
    return DeviceType.AIR_CONDITION;
  }
  if (code.indexOf("CRT_RELAY_") >= 0) {
    return DeviceType.CURTAIN;
  }
  //        if (code.indexOf("RF_RELAY")) {
  //            return CURTAIN;
  //        }
  if (code.indexOf("SCREEN_RELAY") >= 0) {
    return DeviceType.SCREEN;
  }
  //        if (code.indexOf("AIRSWITCH_CONTROL_")) {
  //            return SWITCH_CONTROL;
  //        }
  //        if (code.indexOf("485_RELAY3_")) {
  //            return LIGHT;
  //        }
  if (code.indexOf("LIGHT_RELAY3_") >= 0) {
    return DeviceType.LIGHT;
  }
  if (code.indexOf("BleGate_") >= 0) {
    const arr = code.split("@");
    if (arr.length > 1) {
      const category = parseInt(arr[0]);
      return parseBLE(category);
    }
    return DeviceType.BLE_GATEWAY;
  }
  if (code.indexOf("PLATFORM_") >= 0) {
    return DeviceType.PLATFORM;
  }
  return null;
};
